﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.PBKT;
using Entities.Admin;

namespace WindowsForms.PBKT
{
	public partial class FormCauThuThem : Form
	{
		private CauThu cauThu = null;
		private List<DoiTuyen> dsDT = null;
		private String maDoiTuyen = "";
		public FormCauThuThem(CauThu ct)
		{
			cauThu = ct;
			InitializeComponent();
			dsDT = DoiTuyen.Objects.All().ToList();

		}
		public FormCauThuThem(String maDT)
		{
			maDoiTuyen = maDT;
			InitializeComponent();
			dsDT = DoiTuyen.Objects.All().ToList();

		}

		private void btnLuu_Click(object sender, EventArgs e)
		{
			LuuCauThu();
		}
		void LuuCauThu()
		{
			CauThu ct = new CauThu();
			DiaChi dc = new DiaChi();
			int idDiaChi = 0;
			if (cauThu != null)
			{
				ct.MaCT = cauThu.MaCT;
				dc.IdDiaChi = cauThu.MaDiaChi;
				idDiaChi = cauThu.MaDiaChi;
			}
			else
			{
				ct.MaCT = txtMaCT.Text;
				ct.MaDiaChi = idDiaChi;
				idDiaChi = DiaChi.Objects.AddWithIdentity(dc);
				dc.IdDiaChi = idDiaChi;
				ct.SoBanThang = 0;
			}
			DoiTuyen dt = cbbDoiTuyen.SelectedItem as DoiTuyen;
			bool kt1 = false;
			int outIntT = 0;

			Int32.TryParse(txtTuoi.Text, out outIntT);
			int outIntSA = -1;
			Int32.TryParse(txtSoAo.Text, out outIntSA);
			if (dt != null && outIntT != 0 && outIntSA != 0)
			{

				ct.MaDT = dt.MaDT;
				ct.TenCauThu = txtTenCT.Text;
				int kq = 0;
				Int32.TryParse(txtTuoi.Text, out kq);
				ct.Tuoi = kq;
				Int32.TryParse(txtSoAo.Text, out kq);
				ct.SoAo = kq;
				ct.MaDiaChi = dc.IdDiaChi;
				dc.SoNha = txtSoNha.Text;
				dc.TenDuong = txtTenDuong.Text;
				dc.QuanHuyen = txtQuan.Text;
				dc.TinhTP = txtTinh.Text;
				DiaChi.Objects.Update(dc);
				kt1 = CauThu.Objects.InsertOrUpdate(ct);
				if (kt1)
					MessageBox.Show("Lưu thành công");
				else
					MessageBox.Show("Lưu không thành công");

			}
			else
			{
				MessageBox.Show("Bạn chưa chọn đội tuyển\nHoặc trường Số áo và tuổi bạn nhập sai");
			}




		}
		private void btnThoat_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnReset_Click(object sender, EventArgs e)
		{
			txtMaCT.Text = "";
			txtTenCT.ResetText();
			txtTuoi.ResetText();
			txtSoAo.ResetText();
			txtSoNha.ResetText();
			txtTenDuong.ResetText();
			txtQuan.ResetText();
			txtTinh.ResetText();
			cbbDoiTuyen.ResetText();
			cbbTrangThai.ResetText();
		}

		private void FormCauThuThem_Load(object sender, EventArgs e)
		{
			cbbDoiTuyen.DataSource = dsDT;
			cbbDoiTuyen.ValueMember = "MaDT";
			cbbDoiTuyen.DisplayMember = "TenDT";
			if (cauThu != null)
			{
				txtMaCT.Enabled = false;
				txtMaCT.Text = cauThu.MaCT;
				txtTenCT.Text = cauThu.TenCauThu;
				txtSoAo.Text = cauThu.SoAo.ToString();
				txtTuoi.Text = cauThu.Tuoi.ToString();
				DiaChi dc = DiaChi.Objects.GetById(cauThu.MaDiaChi);
				txtSoNha.Text = dc.SoNha;
				txtTenDuong.Text = dc.TenDuong;
				txtQuan.Text = dc.QuanHuyen;
				txtTinh.Text = dc.TinhTP;
				cbbTrangThai.SelectedIndex = cauThu.TrangThai;

				cbbDoiTuyen.Enabled = false;
				for (int i = 0; i < cbbDoiTuyen.Items.Count; i++)
				{

					DoiTuyen dt = cbbDoiTuyen.Items[i] as DoiTuyen;
					if (dt.MaDT == cauThu.MaDT)
						cbbDoiTuyen.SelectedIndex = i;

				}
			}
			if (maDoiTuyen != "")
			{
				for (int i = 0; i < cbbDoiTuyen.Items.Count; i++)
				{
					DoiTuyen dt = cbbDoiTuyen.Items[i] as DoiTuyen;
					if (dt.MaDT == maDoiTuyen)
						cbbDoiTuyen.SelectedIndex = i;
					cbbDoiTuyen.Enabled = false;
				}
			}

		}
	}
}
